<html>
<head><meta charset="utf-8"><title>equality of two AnonConst · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html">equality of two AnonConst</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211138578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211138578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211138578">(Sep 24 2020 at 14:26)</a>:</h4>
<p>Hello everyone,<br>
I hope this is the correct place to ask, if not don't hesitate to tell me :)</p>
<p>I currently have some free time and decided to hack around in rustc and came over <a href="https://github.com/rust-lang/rust/issues/68436#issuecomment-663190861">this</a> issue on github. So I went ahead and decided to try to implement proposal 2 from the linked comment.<br>
I am aware that anything in this direction would first have to go through some kind of RFC, but my main goal is to understand a bit of the rustc internals :)</p>
<p>So currently I'm stuck at figuring out how to decide if two  <code>AnonConst</code> A and B are "equal".<br>
For simplicity sake I would define equal as follows (which I would say is the most minimal proposal possible):</p>
<ul>
<li>The AST of A equals the AST of B</li>
<li>The substitutions for A are the same as for B</li>
</ul>
<p>I already implemented checking if the substitutions are the same, but am struggling with getting the AST of A, given the corresponding <code>DefId</code>.</p>
<p>If someone has another idea how to define "equal" for A and B I would appreciate some feedback :)</p>
<p>Have a nice day,<br>
Simon</p>



<a name="211138816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211138816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211138816">(Sep 24 2020 at 14:28)</a>:</h4>
<p>The AST is unavailable when the <code>AnonConst</code> comes from a different crate. Only the MIR is available in that case.</p>



<a name="211140894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211140894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211140894">(Sep 24 2020 at 14:41)</a>:</h4>
<p>Okay, maybe I'm mixing something up (there are a lot of different IDs, contexts, etc) and I think an example would help me understand better:</p>
<div class="codehilite"><pre><span></span><code>struct Arr&lt;const N:usize&gt;();

fn foo&lt;const N: usize&gt;() -&gt; Arr&lt;{N-1}&gt; where ={N-1}{
    Arr()
}
</code></pre></div>


<p>(made up syntax)</p>
<p>Now what I want to check is if the part <code>N-1</code> from <code>Arr&lt;{N-1}&gt;</code> is "equal" to the part <code>N-1</code> from <code>where ={N-1}</code>.<br>
Aside from checking that both occurrences of <code>N</code>point to <code>const N: usize</code>(which I already have) I "just " need to make sure the expressions ignoring substitutions are the same.</p>
<p>Hope this helps understanding my problem.</p>



<a name="211141772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211141772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211141772">(Sep 24 2020 at 14:46)</a>:</h4>
<p><span class="user-mention" data-user-id="346034">@Simon Auch</span> me and <span class="user-mention" data-user-id="124288">@oli</span> are already working on it and you can test the current progress with <code>feature(const_evaluatable_checked)</code></p>



<a name="211141800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211141800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211141800">(Sep 24 2020 at 14:47)</a>:</h4>
<p>this is still at a very early stage so I can't really recommend using this feature for anything serious</p>



<a name="211141955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211141955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211141955">(Sep 24 2020 at 14:48)</a>:</h4>
<p>but feel free to experiment and getting confused (error messages aren't great coupled with a lot of actual bugs)</p>



<a name="211142065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211142065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211142065">(Sep 24 2020 at 14:48)</a>:</h4>
<p>comparing the hir/ast is dangerous here, as this relies on typeck resulting in the same code for the same ast</p>



<a name="211142136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211142136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211142136">(Sep 24 2020 at 14:49)</a>:</h4>
<p>which is not obviously true, as two identical hir trees can have a different result if they are in a different generic context</p>



<a name="211142662"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211142662" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211142662">(Sep 24 2020 at 14:53)</a>:</h4>
<p>Yeah, I found that flag and my currently opened file is actually const_evaluatable :)<br>
Correct me if I'm wrong, but shouldn't a different generic context result in a different substitution?</p>



<a name="211142731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211142731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211142731">(Sep 24 2020 at 14:53)</a>:</h4>
<p>yeah, but different substitutions should be fine</p>



<a name="211143099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211143099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211143099">(Sep 24 2020 at 14:56)</a>:</h4>
<p>so we can equate the two consts in</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Foo</span><span class="o">&lt;</span><span class="p">{</span><span class="n">N</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">test</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="211143157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211143157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211143157">(Sep 24 2020 at 14:56)</a>:</h4>
<p>the <code>N + 1</code> in the return type of <code>test</code> has different substs than in <code>Foo</code></p>



<a name="211144429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211144429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211144429">(Sep 24 2020 at 15:04)</a>:</h4>
<p>and where bounds on <code>fn test</code> can change the generated mir for its constants</p>



<a name="211144863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211144863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211144863">(Sep 24 2020 at 15:08)</a>:</h4>
<p>Oh, so the mir/hir/ast for the two occurrences of<code>N+1</code> might be different even though the additional parameter <code>T</code> isn't part <code>N+1</code>?</p>
<p>For the substitutions, would the following work?</p>
<ul>
<li>Let S_A and S_B be the substitutions for A and B.</li>
<li>Compute the intersection S_AB of S_A and S_B</li>
<li>If A and B still have all required substitutions return true else false(not exactly sure how to phrase this in compiler terms)</li>
</ul>



<a name="211146102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211146102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211146102">(Sep 24 2020 at 15:15)</a>:</h4>
<p>yeah, the issue is if <code>ast</code> and <code>hir</code> are identical but the mir is different</p>



<a name="211146128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211146128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211146128">(Sep 24 2020 at 15:15)</a>:</h4>
<p>as the <code>mir</code> is what's actually used to execute a constant</p>



<a name="211146241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211146241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211146241">(Sep 24 2020 at 15:16)</a>:</h4>
<p>The issue is not really substitutions but <code>param_env</code>s, let me try and build an example here.</p>



<a name="211148256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211148256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211148256">(Sep 24 2020 at 15:30)</a>:</h4>
<p>Realy interesting looking through your commits on this topic (even tough I only understand a fraction of the changes right now)</p>



<a name="211148581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211148581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211148581">(Sep 24 2020 at 15:33)</a>:</h4>
<p><span aria-label="shock" class="emoji emoji-1f628" role="img" title="shock">:shock:</span> that's hard, I would have spend more thoughts on this if I knew that people would look at this</p>



<a name="211148712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211148712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211148712">(Sep 24 2020 at 15:34)</a>:</h4>
<p>a lot of places were either guesses of what's needed or strictly incorrect but easier to implement for now</p>



<a name="211148872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211148872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211148872">(Sep 24 2020 at 15:35)</a>:</h4>
<p>Haha, I didn't mean because of your commits in particular, just my general lack of knowledge about the compiler (it's my first real look into the compiler internals, the thought "how complicated could this be" did not stay long). Sorry if that wasn't clear ^^</p>



<a name="211149102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211149102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211149102">(Sep 24 2020 at 15:36)</a>:</h4>
<p>I would love to invite you to help out here, as this is a really interesting topic for me, but I myself am mostly just exploring what exactly is needed here so there isn't a lot I can potentially split of and mentor</p>



<a name="211149375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211149375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211149375">(Sep 24 2020 at 15:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="346034">Simon Auch</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/equality.20of.20two.20AnonConst/near/211148872">said</a>:</p>
<blockquote>
<p>Haha, I didn't mean because of your commits in particular, just my general lack of knowledge about the compiler. Sorry if that wasn't clear ^^</p>
</blockquote>
<p>I didn't read your comment as criticizing my work, so no worries. I often feel that showing people partially unfinished stuff can be nerve wracking</p>



<a name="211149423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211149423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211149423">(Sep 24 2020 at 15:39)</a>:</h4>
<p>I mean what if they realized that I've made a mistake <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span></p>



<a name="211149895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211149895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211149895">(Sep 24 2020 at 15:43)</a>:</h4>
<p>good thing I'm the reviewer then <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span> I've added so much broken code to rustc over time, it feels like half my PRs are just cleaning up my previous mistakes <span aria-label="grinning" class="emoji emoji-1f600" role="img" title="grinning">:grinning:</span></p>



<a name="211149968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211149968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211149968">(Sep 24 2020 at 15:43)</a>:</h4>
<p>I think it took like 10 PRs just to fix all the mistakes in the miri merger PR</p>



<a name="211150121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211150121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211150121">(Sep 24 2020 at 15:44)</a>:</h4>
<p>I've opened like 3 issues about the mess I made of rustdoc diagnostics lol</p>



<a name="211150167"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211150167" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211150167">(Sep 24 2020 at 15:45)</a>:</h4>
<p>I guess we're all in good company</p>



<a name="211150532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211150532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211150532">(Sep 24 2020 at 15:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/equality.20of.20two.20AnonConst/near/211148712">said</a>:</p>
<blockquote>
<p>a lot of places were either guesses of what's needed or strictly incorrect but easier to implement for now</p>
</blockquote>
<p>That is basically exactly what I did too. Start somewhere in the parser and follow the compiler error all the way :)</p>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/equality.20of.20two.20AnonConst/near/211149423">said</a>:</p>
<blockquote>
<p>I mean what if they realized that I've made a mistake <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span></p>
</blockquote>
<p>Well I'm 100% sure I made some mistake and I only added 247 lines of code so far (to my local repo)</p>



<a name="211151811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211151811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211151811">(Sep 24 2020 at 15:56)</a>:</h4>
<p>@lcnr if using mir/hir/ast is dangerous/impossible in this position, do you already have an idea how you want to do this?</p>
<p>Another option that came to my mind (not to say that I think it's a good option) was something like the following:</p>
<div class="codehilite"><pre><span></span><code>struct Arr&lt;const N:usize&gt;();

fn foo&lt;const N: usize&gt;() -&gt; Arr&lt;{M}&gt; where let M ={N-1}{
    Arr()
}
</code></pre></div>


<p>(made up syntax, could also look like this for example: <code>where const M: usize = {N-1}</code>)<br>
Basically introducing a "derived" const generic for which the caller has to make sure its valid. Obvious downside are:</p>
<ul>
<li>the additional variable names introduced</li>
<li>
<p>readability probably suffers due to additional place that can introduce consts<br>
plus side:</p>
</li>
<li>
<p>no mir/hir/ast equality would need to be checked.</p>
</li>
<li>DRY all the way</li>
</ul>



<a name="211151839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211151839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211151839">(Sep 24 2020 at 15:57)</a>:</h4>
<p>using <code>mir</code> is fine</p>



<a name="211151852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211151852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211151852">(Sep 24 2020 at 15:57)</a>:</h4>
<p>that's what we are doing rn</p>



<a name="211152101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211152101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211152101">(Sep 24 2020 at 15:59)</a>:</h4>
<p>your option already somewhat exists if we use a type alias</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">type</span> <span class="nc">Foo</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">];</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Foo</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="211152179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211152179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211152179">(Sep 24 2020 at 15:59)</a>:</h4>
<p>here all instances of <code>N + 1</code> are the same definition (the one in <code>Foo</code>)</p>



<a name="211152317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211152317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211152317">(Sep 24 2020 at 16:00)</a>:</h4>
<p>and this check is <code>a_def.did == b_def.did &amp;&amp; a_substs == b_substs</code></p>



<a name="211152532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211152532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211152532">(Sep 24 2020 at 16:01)</a>:</h4>
<p>which is what we did in <a href="https://github.com/rust-lang/rust/issues/76559">#76559</a> (the first PR)</p>



<a name="211152995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211152995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211152995">(Sep 24 2020 at 16:04)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/76575">#76575</a> is the most relevant PR on how we actually implement the comparision</p>



<a name="211153369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211153369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211153369">(Sep 24 2020 at 16:07)</a>:</h4>
<p>This PR does two things, we now build an abstract representation for generic constants based on their MIR, see <a href="https://github.com/rust-lang/rust/blob/893fadd11a52aa26fc19c67ee1b79f03d6a1bed3/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs#L140-L149">https://github.com/rust-lang/rust/blob/893fadd11a52aa26fc19c67ee1b79f03d6a1bed3/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs#L140-L149</a></p>



<a name="211153443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211153443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211153443">(Sep 24 2020 at 16:07)</a>:</h4>
<p>and compare these abstract representations by recursively checking for equality <a href="https://github.com/rust-lang/rust/blob/893fadd11a52aa26fc19c67ee1b79f03d6a1bed3/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs#L425">https://github.com/rust-lang/rust/blob/893fadd11a52aa26fc19c67ee1b79f03d6a1bed3/compiler/rustc_trait_selection/src/traits/const_evaluatable.rs#L425</a></p>



<a name="211153987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211153987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211153987">(Sep 24 2020 at 16:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/equality.20of.20two.20AnonConst/near/211152532">said</a>:</p>
<blockquote>
<p>which is what we did in <a href="https://github.com/rust-lang/rust/issues/76559">#76559</a> (the first PR)</p>
</blockquote>
<p>If I understand that correctly this still results in post monomorphization errors right?</p>



<a name="211154125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211154125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211154125">(Sep 24 2020 at 16:12)</a>:</h4>
<p>no, it does not</p>



<a name="211154165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211154165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211154165">(Sep 24 2020 at 16:13)</a>:</h4>
<p>these errors happen during typeck</p>



<a name="211154285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211154285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211154285">(Sep 24 2020 at 16:14)</a>:</h4>
<p>so if you can call a method with this feature, that method cannot cause const eval failures</p>



<a name="211154682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211154682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211154682">(Sep 24 2020 at 16:17)</a>:</h4>
<p>Ah I see what confused me. In the test you provided the compile error points at the <code>[N-1]</code>part and for some reason I jumped to conclusions. Maybe I should go and get something to eat ^^'</p>



<a name="211156480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211156480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Auch <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211156480">(Sep 24 2020 at 16:30)</a>:</h4>
<p>The comparison implementation seems like an approach that could work :)<br>
I think I will pull your latest commits tomorrow and tinker around a bit more from there.</p>
<p>In case you find something you could split off and/or mentor feel free to contact me (maybe those overly complicated abstract consts?).<br>
Thanks for all the valuable input :)</p>



<a name="211159835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211159835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211159835">(Sep 24 2020 at 16:56)</a>:</h4>
<p>yeah, the error location is still quite bad</p>



<a name="211159900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/equality%20of%20two%20AnonConst/near/211159900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/equality.20of.20two.20AnonConst.html#211159900">(Sep 24 2020 at 16:57)</a>:</h4>
<p>and we probably have to do some hacks to improve them until we are actually interested in stabilizing this, at which point we probably feel ok doing more complex changes to other parts of the compiler here</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>